{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 需求\n",
    "# 1 小明 体重 75.0 公斤\n",
    "# 2 小明每次 跑步 会减肥 0.5 公斤\n",
    "# 3 小明每次 吃东西 体重增加 1 公斤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "class Person:\n",
    "    def __init__(self, name, weight):\n",
    "        self.name = name\n",
    "        self.weight = weight\n",
    "\n",
    "    def __str__(self):\n",
    "        return \"{}体重{}公斤\".format(self.name, self.weight)\n",
    "\n",
    "    def run(self):\n",
    "        self.weight -= 0.5\n",
    "        print(\"{}跑步后的体重为{}公斤\".format(self.name, self.weight))\n",
    "\n",
    "    def eat(self):\n",
    "        self.weight += 1\n",
    "        print(\"{}跑步后的体重为{}公斤\".format(self.name, self.weight))\n",
    "\n",
    "\n",
    "xiaoming = Person(\"小明\", 75)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明体重75公斤\n"
     ]
    }
   ],
   "source": [
    "print(xiaoming)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明跑步后的体重为74.5公斤\n"
     ]
    }
   ],
   "source": [
    "xiaoming.run()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明跑步后的体重为75.5公斤\n"
     ]
    }
   ],
   "source": [
    "xiaoming.eat()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 需求\n",
    "# 1 房子(House) 有 户型、总面积 和 家具名称列表\n",
    "# – 新房子没有任何的家具\n",
    "# 2 家具(HouseItem) 有 名字 和 占地面积，其中\n",
    "# – 席梦思(bed) 占地 4 平米\n",
    "# – 衣柜(chest) 占地 2 平米\n",
    "# – 餐桌(table) 占地 1.5 平米\n",
    "# 3 将以上三件 家具 添加 到 房子 中\n",
    "# 4 打印房子时，要求输出：户型、总面积、剩余面积、家具名称列表\n",
    "# 剩余面积\n",
    "# 5 在创建房子对象时，定义一个 剩余面积的属性，初始值和总面积相等\n",
    "# 6 当调用 add_item 方法，向房间 添加家具 时，让 剩余面积 -= 家具面积"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "席梦思占地4平米\n",
      "衣柜占地2平米\n",
      "餐桌占地1.5平米\n",
      "**************************************************\n",
      "要添加席梦思占地4平米\n",
      "要添加衣柜占地2平米\n",
      "要添加餐桌占地1.5平米\n",
      "**************************************************\n",
      "户型：两室一厅\n",
      "总面积：60\n",
      "剩余面积：52.5\n",
      "家具：['席梦思', '衣柜', '餐桌']\n"
     ]
    }
   ],
   "source": [
    "# 先开发家具类，被使用到的类通常应该先开发\n",
    "\n",
    "class HouseItem:\n",
    "    \"\"\"\n",
    "    家具类\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self, name, area):\n",
    "        self.name = name\n",
    "        self.area = area\n",
    "\n",
    "    def __str__(self):\n",
    "        return (\"{}占地{}平米\".format(self.name, self.area))\n",
    "\n",
    "\n",
    "class House:\n",
    "    \"\"\"\n",
    "    房子类\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self, house_type, area):\n",
    "        self.house_type = house_type\n",
    "        self.area = area\n",
    "        self.free_area = area\n",
    "        self.item_list = []\n",
    "\n",
    "    def __str__(self):\n",
    "        return (\"户型：{}\\n总面积：{}\\n剩余面积：{}\\n家具：{}\".format(self.house_type, self.area, self.free_area, self.item_list))\n",
    "\n",
    "    def add_item(self, item: HouseItem):\n",
    "        print(\"要添加%s\" % item)\n",
    "        # 1.判断家具面积是否大于剩余面价\n",
    "        if item.area > self.free_area:\n",
    "            print(\"%s 的面积太大，不能添加到房子中\" % item.name)\n",
    "            return\n",
    "        # 2. 将家具的名称追加到名称列表中\n",
    "        self.item_list.append(item.name)\n",
    "        # 3. 计算剩余面积\n",
    "        self.free_area -= item.area\n",
    "\n",
    "\n",
    "# 1. 创建家具\n",
    "bed = HouseItem(\"席梦思\", 4)\n",
    "chest = HouseItem(\"衣柜\", 2)\n",
    "table = HouseItem(\"餐桌\", 1.5)\n",
    "print(bed)\n",
    "print(chest)\n",
    "print(table)\n",
    "print(\"*\" * 50)\n",
    "# 2. 创建房子对象\n",
    "my_home = House(\"两室一厅\", 60)\n",
    "my_home.add_item(bed)\n",
    "my_home.add_item(chest)\n",
    "my_home.add_item(table)\n",
    "print(\"*\" * 50)\n",
    "print(my_home)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "# 需求\n",
    "# 1 士兵 许三多 有一把 AK47\n",
    "# 2 士兵 可以 开火\n",
    "# 3 枪 能够 发射 子弹\n",
    "# 4 枪 装填 装填子弹 —— 增加子弹数量"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "class Gun:\n",
    "    def __init__(self, model):\n",
    "        self.model = model\n",
    "        self.bullet_count = 0\n",
    "\n",
    "    def shoot(self):\n",
    "        if self.bullet_count > 0:\n",
    "            print(\"发射子弹：---->---->----->----->\")\n",
    "            self.bullet_count -= 1\n",
    "            print(\"枪里还剩{}颗子弹\".format(self.bullet_count))\n",
    "        else:\n",
    "            print(\"没有子弹，请填弹\")\n",
    "\n",
    "    def add_bullet(self, count):\n",
    "        print(\"要填%d颗子弹\" % count)\n",
    "        self.bullet_count += count\n",
    "\n",
    "\n",
    "class Soldier:\n",
    "    def __init__(self, name,gun=None):\n",
    "        self.name = name\n",
    "        self.gun = gun\n",
    "\n",
    "\n",
    "    def fire(self):\n",
    "        if self.gun == None:\n",
    "            print(\"{}没有枪！\".format(self.name))\n",
    "            return\n",
    "        else:\n",
    "            self.gun.shoot()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "ak47 = Gun(\"AK47\")\n",
    "# 2. 创建许三多\n",
    "xusanduo = Soldier(\"许三多\",ak47)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "没有子弹，请填弹\n"
     ]
    }
   ],
   "source": [
    "xusanduo.fire()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "要填50颗子弹\n"
     ]
    }
   ],
   "source": [
    "xusanduo.gun.add_bullet(50)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "发射子弹：---->---->----->----->\n",
      "枪里还剩42颗子弹\n",
      "发射子弹：---->---->----->----->\n",
      "枪里还剩41颗子弹\n",
      "发射子弹：---->---->----->----->\n",
      "枪里还剩40颗子弹\n",
      "发射子弹：---->---->----->----->\n",
      "枪里还剩39颗子弹\n",
      "发射子弹：---->---->----->----->\n",
      "枪里还剩38颗子弹\n"
     ]
    }
   ],
   "source": [
    "xusanduo.fire()\n",
    "xusanduo.fire()\n",
    "xusanduo.fire()\n",
    "xusanduo.fire()\n",
    "xusanduo.fire()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}